<?php
/**
 * 检查当前登录用户和权限
 */

$host = '113.44.136.17';
$port = 3306;
$database = 'qiandao';
$username = 'qiandao';
$password = 'NGbks3HWx5PhSxDX';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 检查系统用户 ===\n\n";
    
    // 查询所有用户
    $stmt = $pdo->query("SELECT id, username, nickname, authorize, status, login_at FROM system_user WHERE status = 1 ORDER BY id");
    $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "系统中的用户列表：\n";
    foreach ($users as $user) {
        $isSuper = ($user['username'] === 'admin') ? ' [超级管理员]' : '';
        echo "ID:{$user['id']} | 用户名:{$user['username']} | 昵称:{$user['nickname']} | 最后登录:{$user['login_at']}{$isSuper}\n";
        
        if (!empty($user['authorize'])) {
            echo "  授权角色ID: {$user['authorize']}\n";
            
            // 查询角色信息
            $authIds = explode(',', $user['authorize']);
            foreach ($authIds as $authId) {
                $stmt2 = $pdo->prepare("SELECT id, title, status FROM system_auth WHERE id = ?");
                $stmt2->execute([trim($authId)]);
                $auth = $stmt2->fetch(PDO::FETCH_ASSOC);
                if ($auth) {
                    $status = $auth['status'] == 1 ? '启用' : '禁用';
                    echo "    - 角色: {$auth['title']} ({$status})\n";
                    
                    // 查询角色的权限节点
                    $stmt3 = $pdo->prepare("SELECT node FROM system_node WHERE auth = ?");
                    $stmt3->execute([$auth['id']]);
                    $nodes = $stmt3->fetchAll(PDO::FETCH_COLUMN);
                    if (!empty($nodes)) {
                        echo "      权限节点: " . implode(', ', array_slice($nodes, 0, 5));
                        if (count($nodes) > 5) {
                            echo " ... (共" . count($nodes) . "个)";
                        }
                        echo "\n";
                        
                        // 检查是否有用户管理权限
                        $hasUserPerm = false;
                        foreach ($nodes as $node) {
                            if (strpos($node, 'admin/user') !== false) {
                                $hasUserPerm = true;
                                break;
                            }
                        }
                        if ($hasUserPerm) {
                            echo "      ✓ 有用户管理权限\n";
                        } else {
                            echo "      ❌ 没有用户管理权限\n";
                        }
                    } else {
                        echo "      ❌ 此角色没有分配任何权限节点\n";
                    }
                }
            }
        } else {
            if ($user['username'] !== 'admin') {
                echo "  ❌ 未分配任何角色\n";
            }
        }
        echo "\n";
    }
    
    echo "\n=== 建议 ===\n";
    echo "1. 如果你是用 admin 账号登录，退出后重新登录即可看到菜单\n";
    echo "2. 如果你是用其他账号登录，需要：\n";
    echo "   a) 切换到 admin 账号登录\n";
    echo "   b) 或者在后台给当前账号分配包含用户管理权限的角色\n";
    
} catch (PDOException $e) {
    echo "数据库错误: " . $e->getMessage() . "\n";
}

